<?php
class User{
	private $u_active;
	private $u_login;
	private $u_hash;
	private $u_mail;
	private $u_remove_message = 0;
	private $u_add_message = 0;
	private $u_vote = 0;
	private $u_id;
	private $u_register = false;
	
	//constructeur
	function __construct() {
		$this->u_login = "anonyme";
	}	
	
	//accesseur
	public function __get($value){
		if('login' == $value) {
			return $this->u_login;
		}elseif('remove_message' == $value){
			return 	$this->u_remove_message;
		}elseif('vote' == $value){
			return 	$this->u_vote;
		}elseif('add_message' == $value){
			return 	$this->u_add_message;
		}elseif('register' == $value){
			return 	$this->u_register;
		}elseif('id' == $value){
			return 	$this->u_id;
		}else{
			return "error invalid argument";
		}
	}
	//fonction
	private function setParamUser($login){
		$login = mysql_real_escape_string(htmlspecialchars($login));
		$sql_query = mysql_query("Select id,password,remove_message,add_message,vote,email
									FROM ".DATABASE_TABLE_USER."
									WHERE login = '".$login."' 
									ORDER BY `id` ASC");
		while ($query = mysql_fetch_array($sql_query)){
			$this->u_login = $login;
			$this->u_hash = md5($query['password']+$this->u_login);
			$this->u_id = $query['id'];
			$this->u_remove_message = $query['remove_message'];
			$this->u_add_message = $query['add_message'];
			$this->u_vote = $query['vote'];
			$this->u_email = $query['email'];
			$this->u_register = true;
						
			return true;
		}	
	return false;
	}
	
	public function check_password($login,$password){
		$login = mysql_real_escape_string(htmlspecialchars($login));
		$sql_query = mysql_query("Select password 
									FROM ".DATABASE_TABLE_USER."
									WHERE login = '".$login."' 
									ORDER BY `id` ASC LIMIT 1");
		while ($query = mysql_fetch_array($sql_query)){
			if (md5($password)==$query['password']){
				
				$this->setParamUser($login);
				
				$timestamp_expire = time() + 10*365*24*3600;
				$cookie_value = $this->u_hash;
				setcookie('authen', $cookie_value, $timestamp_expire);
				setcookie('login',$login,$timestamp_expire);				
				return true;		
			}
		}	
	return false;
	}
	
	
	function check_cookie(){
		if (isset($_COOKIE['authen']) and isset($_COOKIE['login']) ){
			$login = mysql_escape_string($_COOKIE['login']);
			$sql_query = mysql_query("Select password from ".DATABASE_TABLE_USER." WHERE login = '".$login."' ORDER BY `id` ASC");
			while ($query = mysql_fetch_array($sql_query)){
				if (md5($query['password']+$login) == $_COOKIE['authen'] ){
					$this->setParamUser($login);
					return true;
				}
			}
		}
		return false;
	}
	
	public function send_email($subject,$body){
		if(mail($this->u_email,$subject,$body)){
			return true;	
		}
		return false;
	}	
	
	public function disconnect_user(){
		$_SESSION['active'] = 0;
		$_SESSION['user'] = false;
		
		setcookie('authen', "false", 1);
		setcookie('login',"false",1);
	}
	
	//Static Fonction
	public static function detect_ie(){
	    if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 8') != true)
	     && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') != false))
	        return true;
	    else
	        return false;
	}
	
	public static function check_user_exist($login,$email){
		$login = mysql_real_escape_string(htmlspecialchars($login));
		$email = mysql_real_escape_string(htmlspecialchars($email));
		$num =mysql_num_rows(mysql_query("SELECT id from ".DATABASE_TABLE_USER."
					WHERE login = '$login' OR email = '$email'"));
		if ($num >=1){
			return true;
		}else{
			return false;
		}
	}
	
	public static function check_email($email){
	if( preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $email)){ 
		  	return true;
	}else{
			return false;
		}
	}
	
	public static function add_user($login,$password,$email){
		$login = mysql_real_escape_string(htmlspecialchars($login));
		$email = mysql_real_escape_string(htmlspecialchars($email));
		$password = md5($password);
		if (User::check_user_exist($login,$email)){
			return false;
		}
		mysql_query("INSERT INTO ".DATABASE_TABLE_USER." (login,password,email,vote,add_message) VALUES('$login', '$password','$email',1,1)");
		return true;
	}
	
	
}
?>